﻿@model IEnumerable<ShenLanCMS.Models.Msgboard>
@{
    ViewBag.Title = "留言板";
    Layout = "~/Views/Shared/_PublicLayout.cshtml";
}
<form id="form1">
<fieldset>
    <legend>留言板</legend>
    <br />
    <div id="tip" style="text-align: center; color: Red; font-size: large;">
    </div>
    <div>
        标题:<input type="text" id="title" />
    </div>
    <div>
        昵称:<input type="text" id="nickname" />
    </div>
    <div>
        邮箱:<input type="text" id="email" /></div>
    <div>
        主页:<input type="text" id="siteurl" /></div>
    <div>
        内容:</div>
    <div>
        <textarea name="msg" id="msg" style="width: 400px; height: 120px;"></textarea>
    </div>
    <input id="btncomment" type="button" value="留言" onclick="sumbitmsg()" />
    <br />
    <hr />
    <fieldset>
        <legend>留言记录</legend>
        <br />
        <div id="showmsg">
        </div>
    </fieldset>
</fieldset>
</form>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function sumbitmsg() {
        if (check()) {
            var title = $("#title").val();
            var nickname = $("#nickname").val();
            var email = $("#email").val();
            var siteurl = $("#siteurl").val();
            var msg = $("#msg").val();
            $.ajax({
                type: "post",
                url: "/msgboard/postmsg?title=" + title + "&nickname=" + nickname + "&email=" + email + "&siteurl=" + siteurl + "&msg=" + msg,
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data == "留言成功") {
                        getmsgboard();
                        $("#title").attr("value", "");
                        $("#nickname").attr("value", "");
                        $("#email").attr("value", "");
                        $("#siteurl").attr("value", "");
                        $("#msg").attr("value", "");
                        $("#tip").html(data);
                    }
                    else
                        $("#tip").html(data);
                }
            });
        }
    }

    getmsgboard();
    function getmsgboard() {
        $.post("/msgboard/getmsgboard", function (data) {
            $("#showmsg").html(data);
        });
    }

    function check() {
        var title = $("#title").val();
        var nickname = $("#nickname").val();
        var email = $("#email").val();
        var siteurl = $("#siteurl").val();
        var msg = $("#msg").val();
        if (title == "") {
            $("#tip").html("请填写标题");
            $("#title").focus();
            return false;
        }
        else {
            if (title.length > 128) {
                $("#tip").html("标题不能超过128个字符");
                $("#title").focus();
                return false;
            }
        }
        if (nickname == "") {
            $("#tip").html("请填写昵称");
            $("#nickname").focus();
            return false;
        }
        else {
            if (nickname.length > 20) {
                $("#tip").html("昵称不能超过20个字符");
                $("#nickname").focus();
                return false;
            }
        }
        if (msg == "") {
            $("#tip").html("请填写内容");
            $("#msg").focus();
            return false;
        }
        else {
            if (msg.length > 1000) {
                $("#tip").html("内容不能超过1000个字符");
                $("#msg").focus();
                return false;
            }
        }
        if (email.length > 64) {
            $("#tip").html("邮箱不能超过64个字符");
            $("#email").focus();
            return false;
        }
        if (siteurl.length > 128) {
            $("#tip").html("主页不能超过128个字符");
            $("#siteurl").focus();
            return false;
        }
        $("#tip").html("");
        return true;
    }
</script>
